package obj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.newdawn.slick.Animation;
import org.newdawn.slick.SlickException;

import play.Things;
import util.Settings;

/**
 * Shooting enemy class.
 * @author VincentLy
 */
public class ShootingEnemy extends Enemy{
	
	private Enemy self;
	private Timer timer;
	private Timer timer2;
	
	public ShootingEnemy(float x,float y,int d,Animation a) throws SlickException{
		super(x,y,d,a);
		self = this;
		timer = new Timer(1000,new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setXVel((float) ((Math.random()*40)-20));
				setYVel((float) ((Math.random()*40)-20));
			}
		});
		timer2 = new Timer(500,new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int x = (int)((Things.player.x()-x())/100);
				int y = (int)((y()-Things.player.y())/100);
				if(Math.abs(y)<1)
					y = 1;
				Things.newprojectiles.add(new Projectile(x(),y(),x,y,1,true,self));
			}
		});
	}
	
	/**
	 * Moves the shooting enemy without regard to gravity.
	 * Shoots bullets and moves once in a while.
	 * @author VincentLy
	 */
	public void update(){
		timer.start();
		timer2.start();
		move((float) (x() + xvel()*Settings.dt),(float) (y()-yvel()*Settings.dt));
	}
	
	/**
	 * Cleans up timers as needed.
	 * @author VincentLy
	 */
	public void clean(){
		if(timer!=null)
			timer.stop();
		if(timer2!=null)
			timer2.stop();
	}
}